package it.asvp.asvp_portal.data.entity.message;

import it.asvp.asvp_portal.data.entity.domain.BaseEntity;
import it.asvp.asvp_portal.data.entity.member.Member;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Formula;

@SuppressWarnings("serial")
@Entity
@Table(name = "topic", catalog = "asvp_db", uniqueConstraints = @UniqueConstraint(columnNames = "title"))
public class Topic extends BaseEntity {

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "id_member", nullable = false)
  private Member       member;

  @Column(name = "title", unique = true, nullable = false, length = 100)
  private String       title;

  @Column(name = "content", nullable = false, length = 1000)
  private String       content;

  @Formula("(SELECT count(*) FROM message m where m.id_topic = id)")
  private long         replyCount;

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "topic")
  @Cascade({ CascadeType.ALL })
  private Set<Message> messages = new HashSet<Message>(0);

  public Topic() {
  }

  public Topic(Member member, String title, String content, Date createdOn, Date lastUpdate) {
    this.member = member;
    this.title = title;
    this.content = content;
    this.createdOn = createdOn;
    this.lastUpdate = lastUpdate;
  }

  public Member getMember() {
    return this.member;
  }

  public void setMember(Member member) {
    this.member = member;
  }

  public String getTitle() {
    return this.title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getContent() {
    return this.content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public long getReplyCount() {
    return replyCount;
  }

  public void setReplyCount(long replyCount) {
    this.replyCount = replyCount;
  }

  public Set<Message> getMessages() {
    return this.messages;
  }

  public void setMessages(Set<Message> messages) {
    this.messages = messages;
  }

}
